#!/usr/bin/env python
#coding=utf8

import paho.mqtt.client as mqtt

trust = "C:/root_cert.pem" #开启TLS时的认证文件目录
user = "iotfreetest/thing01"                            #成功创建thing后返回的username
pwd = "YU7Tov8zFW+WuaLx9s9I3MKyclie9SGDuuNkl6o9LXo="   #成功创建principal后返回的password
endpoint = "01endpoint.mqtt.iot.gz.baidubce.com"       #实例(endpoint)地址
port = 1883                                           #endpoint端口
topic = "test iot service"                             #订阅的主题内容
        
def on_connect(client, userdata, flags, rc):  #连接后返回0为成功
    print("Connected with result code "+str(rc))
    client.subscribe(topic, qos=1) #qos
        
def on_message(client, userdata, msg):
    print("topic:"+msg.topic+" Message:"+str(msg.payload))
        
client = mqtt.Client(
            client_id="test_mqtt_receiver_1", #用来标识设备的ID，用户可自己定义，在同一个实例下，每个实体设备需要有一个唯一的ID
            clean_session=True,
            userdata=None,
            protocol='MQTTv31'
        )
        
# client.tls_insecure_set(True) #检查hostname的cert认证
# client.tls_set(trust) #设置认证文件
client.username_pw_set(user, pwd) #设置用户名，密码
client.on_connect = on_connect #连接后的操作
client.on_message = on_message #接受消息的操作
client.connect(endpoint, port, 60) #连接服务 keepalive=60
client.loop_forever()








#     def xu_sort(self, list):
#         count = len(list)
#         for i in range(0, count-1):
#             min = i
#             for j in range(i+1, count):
#                 if list[min] > list[j]:
#                     min = j
#                     temp = list[i]
#                     list[i] = list[min]
#                     list[min] = temp
#         return list   
#                     
#             
#     def ma_sort(self, list):
#         count = len(list)
#         for i in range(0, count-1):
#             for j in range(i+1, count):
#                 if list[i] > list[j]:
#                     temp = list[j]
#                     list[j]= list[i] 
#                     list[i] = temp
#         return list    